<?php
/*
  Template Name: Carrinho
 */
?>
<?
get_header();
the_post();
?>

<td class="td_carrinho">
	<div id="carrinho">
		<div class="area_carrinho">
			<table border="0" id="prodList">
				<tr class="hbody">
					<td class="nomeProduto">&nbsp;Nome do Produto</td>
					<td class="qtdProduto">Qtd</td>
					<td class="precoProduto">Pre&ccedil;o Unt.</td>
					<td class="totalProduto">Sub Total</td>
				</tr>
				<form action="/carrinho" method="POST" id="quantidade">
					<?
					if (count($_SESSION['produtos'])) {
						foreach ($_SESSION['produtos'] as $id => $arr) {
							$post = get_post($id);
							if (has_post_thumbnail($post->ID)) {
								$default = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large');
								$img = $default[0];
							} else {

								$args = array(
								    'post_type' => 'attachment',
								    'numberposts' => -1,
								    'post_status' => null,
								    'post_parent' => $post->ID
								);

								$attachments = get_posts($args);
								if ($attachments) {
									foreach ($attachments as $attachment) {
										$default = wp_get_attachment_image_src($attachment->ID, 'large');
										break;
									}
									$img = $default[0];
								}
							}

							/* ######################################################################## */
							/* ######################################################################## */
							/* ######################################################################## */

							$base_preco = str_replace(",", ".", $arr['preco']);
							$base_preco = $base_preco * $arr['qtd'];
							$valor_total_pedido = $valor_total_pedido + $base_preco;
							$base_preco = number_format($base_preco, 2, ',', '.');
							$topline = 'topline';

							if ($arr['filtro']) {
								$filtro = get_post_filter_id($arr['filtro']);
								//print_r($filtro);
							}
							?>

							<tr class="linha" id="<?= $id; ?>">
								<td class="nomeProduto <?= $topline; ?>">
									<div class="iProduto left" style="background: url('<?= get_bloginfo("template_url"); ?>/img.php?src=<?= $img; ?>&w=120&h=80&zc=1') no-repeat;"></div>
									<div class="texto left">
										<a href="<?= get_permalink(); ?>" title="<?= get_the_title(); ?>"  alt="<?= get_the_title(); ?>"><?= get_the_title(); ?> <?= $filtro->label; ?></a>
									</div>
									<div class="remover right">
										<a href="?excluir=<?= $post->ID; ?>" title="Remover: <?= get_the_title(); ?>"  alt="Remover: <?= get_the_title(); ?>"><img src="http://icons.iconseeker.com/png/fullsize/aspnet/trash-20.png" border="0" /></a>
									</div>
								</td>
								<td class="qtdProduto <?= $topline; ?>"><input type="text" name="produto[<?= $id; ?>][qtd]" value="<?= $arr['qtd']; ?>" class="qtd" id="<?= $id; ?>"><br /><small class="update"><img src="http://apps.tre-am.jus.br/harpianet/images/reload.png" border="0" title="Atualizar: <?= get_the_title(); ?>"  alt="Atualizar: <?= get_the_title(); ?>" /></small></td>
								<td class="precoProduto <?= $topline; ?>">
									R$ <?= number_format(str_replace(",", ".", $arr['preco']), 2, ',', '.'); ?>
								</td>
								<td class="totalProduto <?= $topline; ?>">R$ <span class="valor_total"><?= $base_preco; ?></span></td>
							</tr>
							<?
						}
						//$valor_total_pedido = $base_frete + $valor_total_pedido;
						$_SESSION['total_pedido'] = $valor_total_pedido;
						?>
						<tr class="hbody total">
							<td class="total_text" colspan="3">Subtotal:</td>
							<td class="total_valor" >R$ <span id="valor_total_pedido"><?= number_format($valor_total_pedido, 2, ',', '.'); ?></span></td>
						</tr>
					</form>
					<tr class="hbody total" id="cep_add_valor">
						<td class="total_text_cep" colspan="3">Digite seu CEP para calcular o frete:</td>
						<td class="total_valor_cep" ><input type="text" id="cep" name="cep" value="" class="border" style="text-align: center; width: 70px;"></td>
					</tr>
					<?
					if (get_user_info('id')) {
						?>
						<tr class="finalizar">
						<form action="/fechar-pedido/" method="POST" enctype="multipart/form-data">
							<?
							if (count($_SESSION['produtos'])) {
								foreach ($_SESSION['produtos'] as $id => $arr) {
									echo "<input type='hidden' name='pedido[" . $id . "]' value='" . $arr['qtd'] . "'>";
								}
							}
							?>
							<td class="comprar" colspan="4" >
								<input type="hidden" name="sentCep" id="sentCep" value="">
								<input type="hidden" name="valor_total" value="<?= number_format($valor_total_pedido, 2, ',', '.'); ?>">
								<input type="submit" name="finalizar" class="finalizar" value="Fechar Pedido" id="finalizar" onclick="jQuery('#sentCep').val(jQuery('#cep').val());" disabled>
							</td>
						</form>
						</tr>

						<?
					} else {
						?>
						<tr class="logando">
							<td class="logar" colspan="4" >
								<input type="button" name="login" class="logando" value="Identifique-se" id="logando" onclick="window.location = '/cadastro';">
							</td>
							</form>
						</tr>
						<?
					}
					?>

					<tr class="comprando">
						<td class="comprar" colspan="4" >
							<input type="button" name="finalizar" class="comprando" value="Continuar Comprando" id="comprando" onclick="window.location = '/';">
						</td>
						</form>
					</tr>
					<?
				} else {
					?>

					<tr>
						<td class="" colspan="4" align="center" >
							<br />
							<br />
							Seu carrinho está vazio!
							<br />
							<br />
						</td>
					</tr>
					<?
				}
				?>
			</table>
		</div> <!--  #content -->
	</div> <!--  #content -->
</td>
<? get_footer(); ?>